﻿#ifndef XQHTTPREQUESTACCOUNT_H
#define XQHTTPREQUESTACCOUNT_H
#include"XQHttpRequestObject.h"
#include"XQHttpAccount.h"
#include"XQAccountData.h"
//推送客户端
class XQHttpRequestAccount :public XQHttpRequestObject
{
	Q_OBJECT
public:
	XQHttpRequestAccount(QObject* parent = nullptr);
	~XQHttpRequestAccount();
	//返回最近一次的错误
	HttpAccount::Error error()const;
	//返回最近一次的结果
	HttpAccount::Result result()const;
	//返回从服务器获取的账号信息
	const XQAccountData& accountData()const;
	//返回当前是否登录了
	bool isLogin()const;
public:
	void setHttp(XQHttpClient* http)override;
	void connectToServer(const QString& url);
	//连接到服务器并等待
	bool connectToServerWait(const QString& url, int msecs = 3_s);
	//等待结果
	bool wait(int msecs = 3_s);
public:
	void Account_NewEmailVerify(const QString& email);//验证码
	void Account_BindEmailVerify(const QString& account, const QString& email);//验证码
	void Account_Login(const QString& account,const QString& password);//登录
	void Account_Logout();//登出
	void Account_Register(const XQAccountData& account, const QString& verify);//注册
	void Account_ModifyPassword(const QString& account, const QString& password, const QString& newPassword);//修改密码
	void Account_ModifyInfo(const XQAccountData& account, const QStringList& fields = QStringList());//修改信息
	void Account_ModifyEmail(const QString& email, const QString& verify, const QString& newEmail, const QString& newVerify);//修改邮箱
	void Account_ModifyEmail(const QString& account, const QString& password, const QString& email, const QString& verify, const QString& newEmail, const QString& newVerify);//修改邮箱
	void Account_portrait(const QString& account);//查询头像
	void Account_Info(const QStringList& fields= QStringList());//信息
	void Account_Info(const QString& account, const QString& password,const QStringList& fields = QStringList());//信息
signals://信号
	//发生错误
	void errorOccurred(HttpAccount::Error error);
	//返回结果
	void resultOccurred(HttpAccount::Result result);
	//全部完成时
	void done();
	//登录
	void login();
	//登出
	void exit();
protected:
	void init()override;
	void httpClient_init()override;
protected:
	//发送数据
	void send(const QByteArray& data= QByteArray())override;
	//将账号插入map中加密
	virtual void account_encrypt(QVariantMap&map, const QString& account);
	//将密码插入map中加密
	virtual void password_encrypt(QVariantMap& map, const QString& password);
	//将新密码插入map中加密
	virtual void newPassword_encrypt(QVariantMap& map, const QString& newPassword);
protected slots:
	//响应处理
	void replyRead(const XQHttpHeadReply& head)override;
	//结果处理
	void resultProcessing(HttpAccount::Result result);
protected:
	XQAccountData m_account;//账号信息
	HttpAccount::Error m_error= HttpAccount::Error::NoError;//错误
	HttpAccount::Result m_result = HttpAccount::Result::null;//返回的结果
	std::atomic<bool> m_Login = false;//当前是登录的吗
	std::atomic<int> m_requestCount=0;//当前还未返回结果的请求数
};
#endif